package com.bjpowernode.exception;

import com.bjpowernode.result.Result;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 处理全局异常
 */
@RestControllerAdvice
public class MyExceptionHandler {

    /**
     * 出现异常之后会执行到该方法中
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){
        e.printStackTrace();
        return Result.FAIL(e.getMessage());
    }

    /**
     * 出现空外键约束异常会执行这里
     */
    @ExceptionHandler(DataIntegrityViolationException.class)
    public Result handleException(DataIntegrityViolationException e) {
        e.printStackTrace();
        return Result.FAIL("外键约束");
    }

}
